package JFrame;

import serive.UserService;
import serive.UserServiceImpl;

import javax.swing.*;
import java.awt.Font;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

/**
 * @author DONG
 */
public class RegisterJframe {
	private JFrame frame;
	private JTextField UId,psw1,pwd2;

	private final UserService userService;
	
	public RegisterJframe() {
		userService=new UserServiceImpl();
		initialize();
		frame.setVisible(true);
	}
	private void initialize() {
		frame = new JFrame();
		frame.getContentPane().setBackground(new Color(27, 217, 202, 255));
		frame.setResizable(false);
		frame.setBounds(100, 100, 731, 601);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
		JLabel lblNewLabel = new JLabel("注册账号");
		lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
		lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 32));
		lblNewLabel.setBounds(107, 50, 481, 37);
		frame.getContentPane().add(lblNewLabel);
		
		JLabel lblNewLabel_1 = new JLabel("用户名：");
		lblNewLabel_1.setFont(new Font("宋体", Font.PLAIN, 20));
		lblNewLabel_1.setBounds(150, 151, 93, 38);
		frame.getContentPane().add(lblNewLabel_1);
		
		UId = new JTextField();
		UId.setBounds(264, 154, 241, 37);
		frame.getContentPane().add(UId);
		UId.setColumns(30);

		JLabel lblNewLabel_2 = new JLabel("密码：");
		lblNewLabel_2.setFont(new Font("宋体", Font.PLAIN, 20));
		lblNewLabel_2.setBounds(150, 216, 61, 38);
		frame.getContentPane().add(lblNewLabel_2);
		
		psw1 = new JTextField();
		psw1.setBounds(264, 282, 241, 37);
		frame.getContentPane().add(psw1);
		psw1.setColumns(30);

		JLabel label = new JLabel("确认密码：");
		label.setFont(new Font("宋体", Font.PLAIN, 20));
		label.setBounds(150, 279, 119, 38);
		frame.getContentPane().add(label);

		pwd2 = new JTextField();
		pwd2.setColumns(30);
		pwd2.setBounds(264, 219, 241, 37);
		frame.getContentPane().add(pwd2);

		JLabel lblNewLabel_3 = new JLabel("邮箱：");
		lblNewLabel_3.setFont(new Font("宋体", Font.PLAIN, 20));
		lblNewLabel_3.setBounds(150, 338, 69, 45);
		frame.getContentPane().add(lblNewLabel_3);

		JTextField textField = new JTextField();
		textField.setColumns(50);
		textField.setBounds(264, 344, 241, 37);
		frame.getContentPane().add(textField);

		JButton btnNewButton = new JButton("注册");
		btnNewButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				String uIdText = UId.getText();
				String psw1Text = psw1.getText();
				String pwd2Text = pwd2.getText();
				String emailText = textField.getText();
				boolean b = userService.register(uIdText, psw1Text, pwd2Text, emailText);
				if (!b){
					JOptionPane.showMessageDialog(frame.getContentPane(), "注册失败,可能用户名已存在或者两次密码不一致\n" +
							"或者用户名必须是6-10位字母、数字、下划线，不能以数字开头\n" +
							"或者密码必须是6-20位的字母、数字、下划线", "注册失败",JOptionPane.WARNING_MESSAGE);
				}else {
					JOptionPane.showMessageDialog(frame.getContentPane(), "注册成功，成功上传服务器", "！！！",JOptionPane.WARNING_MESSAGE);
					frame.setVisible(false);
				}
			}
		});
		btnNewButton.setForeground(Color.black);
		btnNewButton.setBounds(276, 417, 175, 31);
		frame.getContentPane().add(btnNewButton);
	}
}
